package cn.steel.controller.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.steel.base.ResponseData;
import cn.steel.domain.dto.UserDto;
import cn.steel.domain.entity.User;
import cn.steel.domain.vo.UserVo;
import cn.steel.service.UserService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;


@Slf4j
@Controller
@RequestMapping("/manager/user")
@Api(tags = "管理员用户管理")
@PreAuthorize("hasAuthority('ADMIN')")
@RequiredArgsConstructor
public class ManagerUserController {

    private final UserService userService;

    @ApiOperation("显示用户列表页面")
    @GetMapping
    public String userList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String search,
            @RequestParam(required = false) String role,
            @RequestParam(required = false) Integer status,
            Model model) {
        log.info("显示用户列表页面: page={}, size={}, search={}, role={}, status={}", page, size, search, role, status);
        Page<UserVo> pageResult = userService.getUsersPaginated(page, size, search, role, status);
        model.addAttribute("users", pageResult.getResult());
        model.addAttribute("totalPages", pageResult.getPages());
        model.addAttribute("totalItems", pageResult.getTotal());
        model.addAttribute("currentPage", pageResult.getPageNum());
        model.addAttribute("search", search);
        model.addAttribute("role", role);
        model.addAttribute("status", status);
        return "manager/user_list";
    }

    @ApiOperation("获取用户数据以编辑")
    @GetMapping("/user/edit")
    @ResponseBody
    public ResponseData<UserDto> getUserForEdit(@RequestParam Long id) {
        log.info("获取用户数据以编辑: id={}", id);
        User user = userService.getUserById(id);
        if (user == null) {
            return ResponseData.fail(404, "用户不存在");
        }
        UserDto userDto = BeanUtil.toBean(user, UserDto.class);
        return ResponseData.ok(userDto, 200);
    }

    @ApiOperation("修改用户")
    @PostMapping("/user/update")
    public String updateUser(@Valid @ModelAttribute UserDto userDto) {
        log.info("修改用户: userDto={}", userDto);
        User user = BeanUtil.toBean(userDto, User.class);
        userService.updateUser(user);
        return "redirect:/manager/user?success=true";
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete")
    public String deleteUser(@RequestParam Long id) {
        log.info("删除用户: id={}", id);
        userService.deleteUser(id);
        return "redirect:/manager/user?success=true";
    }

    @ApiOperation("切换用户状态")
    @PostMapping("/toggleStatus")
    public String toggleUserStatus(@RequestParam Long id, @RequestParam Integer status) {
        log.info("切换用户状态: id={}, status={}", id, status);
        userService.toggleUserStatus(id, status);
        return "redirect:/manager/user?success=true";
    }
}